clear
set matsize 5000
cd <directory>
**********************************************************************************************************************
** Takes the cleaned weather data (from weatherdata_clean.do) and the cleaned attendance data (from attendancedata_clean.do) and merges them together.
**********************************************************************************************************************

*line-level attendance mean probabilities
use "attendance_merged_tamu".dta", clear
collapse (sum) totp=present (mean) meanp= present, by(unit BATCH date)
coun
destring BATCH, replace force
drop if BATCH==.

merge m:1 unit using "tamu_allunitsmerge", gen(mm)
keep if mm==3
drop mm
g year=year(date)
g month=month(date)
g day=day(date)
g date1=date
drop date
merge m:1 longitude latitude year month day using "weather_2010to2013_allweeklags.dta ", gen(mmm)
keep if mmm==3
drop mmm

g unit1=unit
replace unit=12 if unit==11
merge m:1 unit using "leddetails.dta", gen(m2)
g monthy=year+(month/12)
g monthyled=yearled+(monthled/12)
g led=monthy>=monthyled
bys unit: egen unitled=mean(led)

replace unit=unit1
drop unit1
foreach var of varlist meant himeant wbgtmeant{
g `var'led=`var'*led
replace `var'led=. if `var'==. | led==.
}

g dow=dow(date1)
drop if dow==0
cap drop *meant*led
foreach var of varlist meant himeant wbgtmeant{
g `var'led=`var'*led
replace `var'led=. if `var'==. | led==.
}
cap drop wbgtmeant2
cap drop wbgtmeant1
egen rno=group(unit BATCH)

cap rename wbgtmaxtemperature* wbgtmaxtemp*

cap rename wbgtmintemperature* wbgtmintemp*

save "linelevelattendancewithtamudatanew", replace
